<?php
namespace Weixin\Controller;

use Think\Controller;

class WxSettingController extends Controller {
	public function index(){
    	//当前目录的上上上级目录
    	$url_path =  C ( 'wesetDir' );
    	$xml_path = $url_path."/lib/config/app.xml";

    	if(file_exists($xml_path)) {
    		$app_xml = (array)simplexml_load_file($xml_path);
    		$appId = $app_xml['appId'];
    		$appSecret = $app_xml['appSecret'];
    		
    	} else {
    		$appId = null;
    		$appSecret = null;
    	}
    	
    	$this->assign("appid", $appId);
    	$this->assign("appsecret", $appSecret);
    	$this->display();
    }
    
    public function addSettingApp(){
    	$appid = $_POST['appid'];
    	$appsecret = $_POST['appsecret'];
    	if(!empty($appid) && !empty($appsecret)) {
    		$addAppFlag = $this->createAppXml($appid, $appsecret);
    		
    		$url_path =  C ( 'wesetDir' );
    		
    		if (file_exists_case($url_path.'/lib/config/weixin.config.php')) {
    			$c = require_once $url_path.'/lib/config/weixin.config.php';
    			
    			$c ['APPID'] = $appid;
    			$c ['APPSECRET'] = $appsecret;
    		} else {
    			$c = array(
    				'APPID' => $appid,
    				'APPSECRET' => $appsecret
    			);
    		}
    		
    		$settingstr="<?php \n return array(\n";
    		foreach($c as $key=>$v){
    			$settingstr.= "\t'".$key."'=>'".$v."',\n";
    		}
    		$settingstr.=");\n?>\n";
    		
    		if(file_exists_case($url_path.'/lib/config/weixin.config.php')) {
    			//通过file_put_contents保存weixin.config.php文件；
    			file_put_contents($url_path.'/lib/config/weixin.config.php', $settingstr);
    		} else {
    			$myfile = fopen($url_path.'/lib/config/weixin.config.php', 'w') or die("Unable to open file!");
    			fwrite($myfile, $settingstr);
    			fclose($myfile);
    		}
    		
    		if($addAppFlag) {
    			$this->success("添加开发者凭证成功！");
    		} else {
    			$this->error("添加开发者凭证失败！");
    		}
    	}
    	
    }
    
    public function wxServiceSetting(){
    	$this->display();
    }
    
    public function wxVisitor(){
    	//微信关注
    	$url_path = dirname(dirname(__FILE__));
    	$url_path =  C ( 'wesetDir' ) ;
    	$xml_path = $url_path."/lib/config/attention.xml";
    	if(file_exists($xml_path)) {
    		$attention_xml = (array)simplexml_load_file($xml_path);
    		$attention = $attention_xml['attention'];
    	} else {
    		$attention = null;
    	}
    	$this->assign("attention", $attention);
    	$this->display();
    }
 
    
    public function addAttention(){
    	
    	$attention = $_POST['attention'];
    	if(!empty($attention)) {
    		$addAppFlag = $this->createAttentionXml($attention);
    		if($addAppFlag) {
    			$this->success('添加关注推送信息成功！');
    		} else {
    			$this->error('添加关注推送信息失败！');
    		}
    	}
    }
    
    /**
     *
     * @param  $appId $appSecret
     *
     */
    public function createAppXml($appId, $appSecret) {
    
    	$doc = new \DOMDocument("1.0","utf-8");
    	$doc -> formatOutput=true; // 设置可输出操作
    	$root = $doc->createElement("root"); //设置根节点
    	$root = $doc->appendChild($root); //添加根节点
    	$name=$doc->createElement("appId");  //创建父节点name
    	$name=$root->appendChild($name);    //把节点添加到menu节点的子节点
    	$name->appendChild($doc->createTextNode($appId));
    
    	$type=$doc->createElement("appSecret");  //创建父节点name
    	$type=$root->appendChild($type);    //把节点添加到menu节点的子节点
    	$type->appendChild($doc->createTextNode($appSecret));
    	// 			$file_path = dirname(__FILE__);
    	$file_path = C ( 'wesetDir' );
    
    	try{
    		$doc->save($file_path."/lib/config/app.xml");
    		return true;
    	} catch(Exception $e){
    		return false;
    	}
    }
    
    /**
     * @param  $appId $appSecret
     */
    public function createServerXml($url, $token) {
    	$doc = new \DOMDocument("1.0","utf-8");
    	$doc -> formatOutput=true; // 设置可输出操作
    	$root = $doc->createElement("root"); //设置根节点
    	$root = $doc->appendChild($root); //添加根节点
    	$name=$doc->createElement("url");  //创建父节点name
    	$name=$root->appendChild($name);    //把节点添加到menu节点的子节点
    	$name->appendChild($doc->createTextNode($url));
    
    	$type=$doc->createElement("token");  //创建父节点name
    	$type=$root->appendChild($type);    //把节点添加到menu节点的子节点
    	$type->appendChild($doc->createTextNode($token));
    	try{
    		$doc->save("url.xml");
    		return true;
    	} catch(Exception $e){
    		return false;
    	}
    }
    /**
     * @param  $appId $appSecret
     */
    public function createAttentionXml($attention_text) {
    	$doc = new \DOMDocument("1.0","utf-8");
    	$doc -> formatOutput=true; // 设置可输出操作
    	$root = $doc->createElement("root"); //设置根节点
    	$root = $doc->appendChild($root); //添加根节点
    	$name=$doc->createElement("attention");  //创建父节点name
    	$name=$root->appendChild($name);    //把节点添加到menu节点的子节点
    	$name->appendChild($doc->createTextNode($attention_text));
    	// 			$file_path = dirname(__FILE__);
    	$file_path =C ( 'wesetDir' );
    	try{
    		$doc->save($file_path."/lib/config/attention.xml");
    		return true;
    	} catch(Exception $e){
    		return false;
    	}
    }
}